﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Devices.DeviceEntity
{
    public partial class TS : DeviceBase
    {
        string redId;
        string greenId;
        string yellowId;
        string turnId;

        public string RedId
        {
            get
            {
                return redId;
            }

            set
            {
                redId = value;
            }
        }

        public string GreenId
        {
            get
            {
                return greenId;
            }

            set
            {
                greenId = value;
            }
        }

        public string YellowId
        {
            get
            {
                return yellowId;
            }

            set
            {
                yellowId = value;
            }
        }

        public string TurnId
        {
            get
            {
                return turnId;
            }

            set
            {
                turnId = value;
            }
        }

        public override string StatusDescription
        {
            get
            {
                return base.StatusDescription;
            }

            set
            {
                switch (value)
                {
                    case "红灯": this.Image = ResPic.车指禁止; break;
                    case "黄灯": this.Image = ResPic.红绿灯黄灯; break;
                    case "绿灯": this.Image = ResPic.车指通行; break;
                    case "左转": this.Image = ResPic.红绿灯左转; break;
                    case "故障": this.Image = ResPic.车指故障; break;
                }
                base.StatusDescription = value;
            }
        }


        public override string DeviceId
        {
            get
            {
                return base.DeviceId;
            }

            set
            {
                base.DeviceId = value;
                ReadParam();
            }
        }
        

        public TS()
        {
            InitializeComponent();
        }

        public async void ReadParam()
        {
            DataTable dtParamIDTable = await DataDeal.ReadParamAsync(DeviceId);
            foreach(DataRow item in dtParamIDTable.Rows)
            {
                switch(item["ParamStatus"].ToString())
                {
                    case "GREEN": GreenId = item["ParamName"].ToString(); break;
                    case "RED": RedId = item["ParamName"].ToString(); break;
                    case "YELLOW": YellowId = item["ParamName"].ToString(); break;
                    case "TURN": TurnId = item["ParamName"].ToString(); break;
                }
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public override void SendCommand(CommandType commandType)
        {
            switch (commandType)
            {
                case CommandType.交通信号灯红: SendRed(); break;
                case CommandType.交通信号灯黄: SendYellow(); break;
                case CommandType.交通信号灯绿: SendGreen(); break;
                case CommandType.交通信号灯左: SendTurn(); break;
            }
        }

        private void SendRed()
        {
            DataDeal.SendCommand(DeviceId, RedId, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "设置红灯");
        }
        private void SendYellow()
        {
            DataDeal.SendCommand(DeviceId, YellowId, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "设置黄灯");
        }
        private void SendGreen()
        {
            DataDeal.SendCommand(DeviceId, GreenId, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "设置绿灯");
        }
        private void SendTurn()
        {
            DataDeal.SendCommand(DeviceId, TurnId, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "设置左转");
        }

        private void TS_DoubleClick(object sender, EventArgs e)
        {
            TSForm f = new TSForm(this);
            f.ShowDialog();
        }
    }
}
